<extend name="Public:base" />
<block name="body_content">
    <div class="app app-header-fixed <if condition='$Think.config.page-boxed eq 1'>container</if>">
        <!-- header -->
        <include file="Public:header" />
        <!-- /header -->

        <!-- aside -->
        <include file="Public:sidebar" />
        <!-- /aside -->

        <!-- content -->
        <div id="content" class="app-content" role="main">
            <div class="app-content-body ">
                <div class="bg-light lter b-b wrapper-md">
                    <h1 class="m-n font-thin h3">{$meta_title}</h1>
                </div>
                <div class="wrapper-md">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="panel panel-default">
                                <div class="panel-heading font-bold">{$Think.lang.Gateway_ApiInfo}</div>
                                <div class="panel-body">
                                    <div class="form-group">
                                        <label>{$Think.lang.Gateway_SelectSubType}</label>
                                        <select id="gtw_type" onchange="changeSubType(this)" class="form-control m-b">
                                            <option>----{$Think.lang.Gateway_SelectSubType}-----</option>
                                            <?php   for($i=0;$i<count($types);$i++) { ?>
                                            <option value="<?php echo $types[$i]['id']?>"><?php $friend_l2 = json_decode($types[$i]['friend_name'],true);echo $friend_l2[LANG_SET]; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <?php for($i=0;$i<count($types);$i++) { ?>
                                    <form role="form" class="InputG" id="Input<?php echo $types[$i]['id'];?>" style="display: none;" onsubmit="return false;">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label>{$Think.lang.Gateway_InputGatewayNickName}</label>
                                                <input type="text" class="form-control" id="gtw_friend_name" name="gtw_friend_name" placeholder="{$Think.lang.Global_PleaseEnter} {$Think.lang.Gateway_InputGatewayNickName}">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label>{$Think.lang.Gateway_InputGatewayName}</label>
                                                <input type="text" class="form-control" id="gtw_name" name="gtw_name" placeholder="{$Think.lang.Global_PleaseEnter} {$Think.lang.Gateway_InputGatewayName}" value="<?php echo $types[$i]['name'].date('YmdHs');?>">
                                            </div>
                                        </div>
                                        <?php $input = json_decode($types[$i]['input'],true); foreach ($input as $key=>$value) {?>
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <?php if ($value['type'] == "text") {?>
                                                <label><?php echo $value['display_name'][LANG_SET];?></label>
                                                <input type="<?php echo $value['type'];?>" class="form-control" name="<?php echo $key;?>" placeholder="<?php echo $value['description'][LANG_SET];?>">
                                                <?php } elseif ($value['type'] == "textarea") {?>
                                                <label><?php echo $value['display_name'][LANG_SET];?></label>
                                                <textarea class="form-control" name="<?php echo $key;?>" placeholder="<?php echo $value['description'][LANG_SET];?>"></textarea>
                                                <?php } else {}?>
                                            </div>
                                        </div>
                                        <?php }?>
                                        <div class="line line-dashed b-b line-lg pull-in"></div>
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label class="col-sm-4 control-label">{$Think.lang.Gateway_SupportPhone}</label>
                                                <div class="col-sm-8">
                                                    <label class="i-switch m-t-xs m-r">
                                                        <input type="checkbox" id="gtw_SupportPhone" name="gtw_SupportPhone" checked="">
                                                        <i></i>
                                                    </label>
                                                </div>
                                            </div>
                                        </div>
                                        <!--<div class="line line-dashed b-b line-lg pull-in"></div>-->
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label class="col-sm-4 control-label">{$Think.lang.Gateway_RequirePhone}</label>
                                                <div class="col-sm-8">
                                                    <label class="i-switch m-t-xs m-r">
                                                        <input type="checkbox" id="gtw_RequirePhone" name="gtw_RequirePhone" onchange="requirePhone(this);">
                                                        <i></i>
                                                    </label>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="line line-dashed b-b line-lg pull-in"></div>
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label>{$Think.lang.Gateway_Status}</label>
                                                <select id="gtw_status" name="gtw_status" class="form-control m-b">
                                                    <option value="ACTIVE">{$Think.lang.Gateway_StatusACTIVE}</option>
                                                    <option value="INACTIVE">{$Think.lang.Gateway_StatusINACTIVE}</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label>{$Think.lang.Gateway_Access}</label>
                                                <select id="gtw_access" name="gtw_access" class="form-control m-b">
                                                    <option value="PUBLIC">{$Think.lang.Gateway_AccessPUBLIC}</option>
                                                    <option value="PRIVATE">{$Think.lang.Gateway_AccessPRIVATE}</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="line line-dashed b-b line-lg pull-in"></div>
                                        <div class="col-lg-12">
                                            <button type="submit" name="saveSubmit" onclick="saveSubmitAction('create',{$types.$i.id});" class="btn btn-primary"><i class="fa fa-check"></i> {$Think.lang.Global_Submit}</button>
                                        </div>
                                    </form>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- /content -->

        <!-- footer -->
        <include file="Public:footer" />
        <!-- /footer -->
    </div>
</block>
<block name="custom_css">
</block>
<block name="custom_js">
<script>
    function changeSubType(obj){
        var opt = obj.options[obj.selectedIndex];
        $(".InputG").hide();
        $("#Input"+opt.value).show();
    }
    function requirePhone(obj) {
        if (obj.checked==true){
            $("#gtw_SupportPhone").prop('checked', true);
        }
    }
    function saveSubmitAction(actionType,type_id) {
        var form_data = JSON.stringify($("#Input"+type_id).serializeArray());
        swal({
            title: "{$Think.lang.Global_Submit}",
            text: "{$Think.lang.Gateway_Submitting}",
            type: "info"
        });
        $.ajax({
            type: "POST",
            url: "/Gateway/actionEditSave?action="+actionType,
            dataType: "json",
            data: {"form_data":form_data,"type_id":type_id},
            timeout: 10000,
            beforeSend: function(){
                swal({
                    title: "{$Think.lang.Global_Submit}",
                    text: "{$Think.lang.Gateway_Submitting}",
                    type: "info"
                });
            },
            success: function(json){
                if(json.success==1){
                    swal({
                        title: "{$Think.lang.Global_Success}",
                        text: json.msg,
                        type: "success",
                        timer: 2000
                    });
                    setTimeout(window.location='/gateway/view?type={$Think.get.type}',2000);
                    return false;
                } else if (json.error==1) {
                    swal({
                        title: "{$Think.lang.Global_ErrorSmall}",
                        text: json.msg,
                        type: "error",
                        timer:2000,
                    });
                    return false;
                }
            },
            error: function () {
                swal({
                    title: "{$Think.lang.Global_Error}",
                    text: "{$Think.lang.Gateway_SubmitError}",
                    type: "info"
                });
                return false;
            }
        });
    }
</script>
</block>